package org.example.fronted.controller;


import org.example.entity.Goods;
import org.example.entity.User;
import org.example.fronted.service.CartService1;
import org.example.fronted.service.FrontedGoodService;
import org.example.fronted.utils.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@CrossOrigin("*")
@RestController
@RequestMapping("/shoppingCart")
public class CartController {

    @Resource
    private FrontedGoodService frontedGoodService;

    private CartService1 cartService1;

    public CartController(CartService1 cartService1) {
        this.cartService1 = cartService1;
    }


    @RequestMapping("/cart")
    public Object getGoodsInfo(String idsStr) {
        //将前端出入过来的ids拆分为数组, 1001,1002,1003
        String[] idsArr = idsStr.split(",");
        // String[] idsArr = {"1001","1002","1003"};

        //生成长度和字符串数组长度一样的整数数组
        Integer[] ids = new Integer[idsArr.length];

        for (int i = 0; i < ids.length; i++) {
            ids[i] = Integer.parseInt(idsArr[i]);
        }

        List<Goods> goodsList = frontedGoodService.setGoodsByIds(ids);

        return goodsList;
    }


    @PutMapping("/add/{goodID}/{uId}")
    public Object addGood(@PathVariable("goodID") String goodId,
                          @PathVariable("uId") String userId){
        Map<String,Object> map=null;
        try {
            System.out.println("goodId_________"+goodId);
            int i = cartService1.addGood(goodId, Integer.parseInt(userId));
            System.out.println("i="+i);
            if (i>0){
                map = MapUtils.getSuccessMap(null);
            }else {
                map = MapUtils.getErrorMap("增加商品失败",i);
            }
        }
        catch (Exception e){
            e.printStackTrace();
            System.out.println("添加商品控制层异常");
        }
        return map;
    }



    @PutMapping("/sub/{goodID}/{uId}")
    public Object subGood(@PathVariable("goodID") String goodId,
                          @PathVariable("uId") String userId){
        Map<String,Object> map=null;
        try {
            int i = cartService1.subGood(Integer.parseInt(goodId), Integer.parseInt(userId));
            if (i>0){
                map = MapUtils.getSuccessMap(null);
            }else {
                map = MapUtils.getErrorMap("削减商品失败",i);
            }
        }
        catch (Exception e){
            e.printStackTrace();
        System.out.println("控制层异常");
        }
        return map;
    }


    @DeleteMapping("/{goodID}/{uId}")
    public Object deleteAllGoods(@PathVariable("goodID") String goodId,
                                 @PathVariable("uId") String userId) {
        Map<String,Object> map=null;
        try {
            int i = cartService1.deleteAllGoods(goodId, Integer.parseInt(userId));
            if (i>0){
                map = MapUtils.getSuccessMap(null);
            }else {
                map = MapUtils.getErrorMap("删除失败",i);
            }

        }catch (Exception e){
            e.printStackTrace();
            System.out.println("控制层异常");
        }
        return map;
    }

    //同步数据库数据
    @PostMapping(value = "syncCart")
    public Map<String, Integer> syncCart(@RequestBody Map<String, Integer> cart, HttpSession session) {
        User u = (User) session.getAttribute("user");
        Map<String, Integer> map = cartService1.syncCart(cart, u.getUId());
        return map;
    }
}
